package org.infosys.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.infosys.basic.utils.AjaxResult;
import org.infosys.basic.utils.PageList;
import org.infosys.org.domain.Department;
import org.infosys.org.query.DepartmentQuery;
import org.infosys.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
* @Title:
* @Description: 部门接口
* @Author: hank
* @Version: 1.0
* @Date:  2022/6/24 23:45
* @Parameters:
* @Return
*/
@RestController
@Api(value = "部门的API",description="部门相关的CRUD功能")
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
    * @Title: loadById
    * @Description: 根据id查询部门信息
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/6/24 23:36
    * @Parameters: [id]
    * @Return org.infosys.org.domain.Department
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department loadById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id) {
        return departmentService.loadById(id);
    }

   /**
   * @Title: loadAll
   * @Description: 查询部门所有信息
   * @Author: hank
   * @Version: 1.0
   * @Date:  2022/6/24 23:46
   * @Parameters: []
   * @Return java.util.List<org.infosys.org.domain.Department>
   */
    @GetMapping
    public List<Department> loadAll() {
        return departmentService.loadAll();
    }

    /**
    * @Title: remove
    * @Description: 根据id删除部门信息
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/6/24 23:48
    * @Parameters: [id]
    * @Return org.infosys.basic.utils.AjaxResult
    */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙，请稍后重试!").setSuccess(false);
        }
    }

    /**
    * @Title: addOrUpt
    * @Description: 新增和修改
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/6/24 23:50
    * @Parameters: [department]
    * @Return org.infosys.basic.utils.AjaxResult
    */
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Department department) {
        try {
            if (ObjectUtils.isEmpty(department.getId())) {
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙，请稍后重试!").setSuccess(false);
        }
    }

    /**
    * @Title: queryDepartment
    * @Description: 条件分页查询
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/6/24 23:59
    * @Parameters: [departmentQuery]
    * @Return org.infosys.basic.utils.PageList<org.infosys.org.domain.Department>
    */
    @PostMapping
    public PageList<Department> queryDepartment(@RequestBody DepartmentQuery departmentQuery) {
        PageList<Department> data = departmentService.loadQuery(departmentQuery);
        return data;
    }

}
